package com.fastloan.utils;

import com.alibaba.fastjson.JSONObject;
import com.fastloan.mgt.invest.weixin.model.token.SaveToken;
import com.fastloan.mgt.invest.weixin.model.token.TokenModel;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author rankai
 *         createTime 2017-12-2017/12/8 15:39
 */
public class WeixinUtil {

    private static final Logger log = LoggerFactory.getLogger(WeixinUtil.class);

    public static final String WEIXIN_TOKEN_API = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    public static final String WEIXIN_JS_API = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";

    public static final String JSCODE_SESSION = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    private static final Map<String, SaveToken> TOKEN_MAP = new HashMap<>();

    public static String getAccessToken(String appId, String secret) {
        SaveToken saveToken = TOKEN_MAP.get(secret);
        if (saveToken != null && saveToken.getToken() != null && timeOut(saveToken.getCreate())) {
            return saveToken.getToken();
        }
        //定义url
        StringBuffer stringBuffer = new StringBuffer(WEIXIN_TOKEN_API);
        stringBuffer.append("&appid=").append(appId).append("&secret=").append(secret);
        String json = HttpUtil.get(stringBuffer.toString());
        TokenModel tokenModel;
        if (json != null) {
            tokenModel = JSONObject.parseObject(json, TokenModel.class);
        } else {
            log.debug("access token 获取没有返回值");
            return null;
        }
        saveToken = new SaveToken();
        saveToken.setCreate(new Date().getTime());
        saveToken.setToken(tokenModel.getAccessToken());
        TOKEN_MAP.put(secret, saveToken);
        return tokenModel.getAccessToken();
    }

    public static String getOpenId(String appId, String secret, String code) {
        StringBuffer stringBuffer = new StringBuffer(JSCODE_SESSION);
        stringBuffer.append("&appid=").append(appId).append("&secret=").append(secret).append("&js_code=").append(code);
        String s = HttpUtil.get(stringBuffer.toString());
        if (StringUtils.isNotBlank(s)) {
            JSONObject jsonObject = JSONObject.parseObject(s);
        }
        return "";
    }

    public static String getJsapi(String appId, String secret) {
        StringBuffer stringBuffer = new StringBuffer(WEIXIN_JS_API);
        stringBuffer.append("&access_token=").append(getAccessToken(appId, secret));
        String json = HttpUtil.get(stringBuffer.toString());
        if (StringUtils.isNotBlank(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            log.debug("getJsapi is {}", jsonObject);
            return jsonObject.getString("ticket");
        } else {
            log.debug("getJsapi is null");
        }
        return "";
    }


    private static boolean timeOut(long time) {
        long newTime = new Date().getTime();
        if (newTime - time > 7200 * 1000) {
            return false;
        }
        return true;
    }
}
